package com.cnzha.qiniu;

import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.io.IOException;

/**
 * @Author: zhaoxin
 * @email: zdkk@foxmail.com
 * @Date: 2020/4/10 13:35
 */
public class SimpleUpload {
    // 设置需要操作的账号的AK和SK
    private static final String ACCESS_KEY = "";
    private static final String SECRET_KEY = "";

    // 要上传的空间名
    private static final String bucketname = "paquw-com";

    // 密钥
    private static final Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);

    //上传
    public static String upload(File file, String key) throws IOException {
        // 创建上传对象，Zone*代表地区
        Configuration configuration = new Configuration(Zone.zone2());
        UploadManager uploadManager = new UploadManager(configuration);
        try {
            // 调用put方法上传
            String token = auth.uploadToken(bucketname);
            if (StringUtils.isEmpty(token)) {
                System.out.println("未获取到token，请重试！");
                return null;
            }
            System.out.println("File name = " + file.getName());
            Response res = uploadManager.put(file, key, token);
            // 打印返回的信息
            if (res.isOK()) {
                return key;
            }
        } catch (QiniuException e) {
            Response r = e.response;
            // 请求失败时打印的异常的信息
            e.printStackTrace();
            System.out.println("error " + r.toString());
            try {
                // 响应的文本信息
                System.out.println(r.bodyString());
            } catch (QiniuException e1) {
                System.out.println("error " + e1.error());
            }
        }
        return null;
    }


    public static void main(String[] args) throws IOException {

        File file = new File("D:\\IIMMGG\\img\\sgame");
//        File file = new File("d:\\IIMMGG\\");
        File[] files = file.listFiles();
        for (File file1 : files) {
            if (!file1.isFile()) {
                File[] listFiles = file1.listFiles();
                for (File listFile : listFiles) {
                    String key = listFile.getParentFile().getName() + "/" + listFile.getName();
                    String upload = upload(listFile, key);
                    System.out.println("upload = " + upload);
                }
            }
        }

    }

}
